﻿
using Module.FlowChart.RunAble.Code.Runner;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Module.FlowChart.RunAble.Code.View
{
    internal partial class Form_Vars : Form, IVarShower
    {
        string funcname = "Non";
        bool IsShowing;
        int VisiblePageCount;
        List<RunningContext> contexts = new List<RunningContext>();
        List<TabPage> pages = new List<TabPage>();
        public Form_Vars()
        {

            InitializeComponent();
            this.TopLevel = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.SetOnSizeChanged((s, e) =>
            {

            });
        }

        public string FuncName { get => funcname; set => funcname = value; }

        TabPage CreatTabPage()
        {
            TabPage re;
            re = new TabPage();
            DataGridView grid = new DataGridView();
            grid.Name = "GRID";
            DataGridViewTextBoxColumn Column_Name = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn Column_Type = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn Column_Value = new DataGridViewTextBoxColumn();
            re.Controls.Add(grid);
            re.Padding = new Padding(3);
            re.Name = "";
            re.UseVisualStyleBackColor = true;

            grid.RowHeadersWidth = 62;
            grid.RowTemplate.Height = 30;
            grid.Dock = DockStyle.Fill;
            grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            grid.Columns.AddRange(new DataGridViewColumn[] {
                 Column_Name,
                Column_Type,
                Column_Value});
            // 
            // Column_Value
            // 
            Column_Value.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            Column_Value.HeaderText = "值";
            Column_Value.MinimumWidth = 8;
            Column_Value.Name = "Column_Value";
            Column_Value.ReadOnly = true;
            // 
            // Column_Type
            // 
            Column_Type.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            Column_Type.HeaderText = "类型";
            Column_Type.MinimumWidth = 8;
            Column_Type.Name = "Column_Type";
            Column_Type.ReadOnly = true;
            // 
            // Column_Name
            // 
            Column_Name.Frozen = true;
            Column_Name.HeaderText = "名称";
            Column_Name.MinimumWidth = 8;
            Column_Name.Name = "Column_Name";
            Column_Name.ReadOnly = true;
            Column_Name.Width = 150;
            return re;
        }
        public void Add(RunningContext context)
        {
            this.contexts.Add(context);
            if (IsShowing)
            {
                if (context.Name == funcname)
                {
                    while (pages.Count < contexts.Count)
                    {
                        pages.Add(CreatTabPage());
                    }
                    pages[VisiblePageCount].Text = context.Depth + "";
                    pages[VisiblePageCount].Parent = tabControl1;
                    var grid = pages[VisiblePageCount].Controls.Find("GRID", false)[0] as DataGridView;
                    foreach (var item in context.vars)
                    {
                        grid.Rows.Add(item.Key, item.Value.Type.ToString(), item.Value.ToString());
                    }
                    VisiblePageCount += 1;
                }
            }
        }
        public void Add(ICollection<RunningContext> contexts)
        {
            foreach (var item in contexts)
            {
                Add(item);
            }
        }

        public void RemoveLast()
        {
            if (funcname == contexts[contexts.Count - 1].Name)
            {
                pages[VisiblePageCount - 1].Parent = null;
                var grid = pages[VisiblePageCount - 1].Controls.Find("GRID", false)[0] as DataGridView;
                grid.Rows.Clear();
                VisiblePageCount -= 1;
            }
            contexts.RemoveAt(contexts.Count - 1);
        }

        public void RemoveAll()
        {
            contexts.Clear();
            VisiblePageCount = 0;
            foreach (var item in pages)
            {
                if (item.Parent != null)
                {
                    item.Parent = null;
                    var grid = item.Controls.Find("GRID", false)[0] as DataGridView;
                    grid.Rows.Clear();
                }
            }
        }

        public void Show(string name, Control holder)
        {
            if (IsShowing)
                HideView();
            IsShowing = true;
            this.Parent = holder;
            holder.SizeChanged += OnContainerSizeChanged;

            funcname = name;
            var show = contexts.Where(a => a.Name == name);
            while (pages.Count < show.Count())
                pages.Add(CreatTabPage());
            int i = 0;
            foreach (var ct in show)
            {
                pages[i].Text = ct.Depth.ToString();
                var grid = pages[i++].Controls.Find("GRID", false)[0] as DataGridView;
                foreach (var item in ct.vars)
                {
                    grid.Rows.Add(item.Key, item.Value.Type.ToString(), item.Value.ToString());
                }
            }
            for (i = 0; i < show.Count(); i++)
                pages[i].Parent = tabControl1;
            for (i = show.Count(); i < pages.Count; i++)
                pages[i].Parent = null;
            VisiblePageCount = show.Count();
            this.Visible = true;
            OnContainerSizeChanged(null, null);
        }

        public void UpdateLast()
        {
            if (contexts.Count == 0)
                return;
            RunningContext context = contexts[contexts.Count - 1];
            if (context.Name != funcname)
                return;
            var grid = pages[VisiblePageCount - 1].Controls.Find("GRID", false)[0] as DataGridView;
            grid.Rows.Clear();
            foreach (var item in context.vars)
            {
                grid.Rows.Add(item.Key, item.Value.TypeName(), item.Value.ToString());
            }

        }

        public void HideView()
        {
            this.Hide();
            if (this.Parent != null)
                this.Parent.SizeChanged -= OnContainerSizeChanged;
            Visible = false;
            IsShowing = false;
            funcname = "Non";
            for (int i = 0; i < VisiblePageCount; i++)
            {
                pages[i].Parent = null;
                var grid = pages[i].Controls.Find("GRID", false)[0] as DataGridView;
                grid.Rows.Clear();
            }
            VisiblePageCount = 0;
        }

        void OnContainerSizeChanged(object s, EventArgs e)
        {
            if (!IsShowing)
                return;
            Size = new Size(Parent.ClientSize.Width / 2, Parent.ClientSize.Height / 3);
            Location = new Point(0, Parent.ClientSize.Height * 2 / 3);
        }

        public void ShowOrUpdateLast(string name, Control holder)
        {
            if (name == funcname && Visible)
            {
                UpdateLast();
            }
            else
            {
                Show(name, holder);
            }
        }
    }
}
